package cn.eveo.refreshandloadmore.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ListView;

import in.srain.cube.views.GridViewWithHeaderAndFooter;
import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.indicator.PtrIndicator;

/**
 * Created by Administrator on 2016/9/22.
 */

public class PtrWrapGridView extends PtrFrameLayout {

    private boolean isLoading = false;
    private boolean loadMoreEnabled = false;

    private PtrOnRefreshListener onRefreshListener;
    private PtrEHeader mPtrClassicHeader;
    private LoadMoreView loadMoreView;
    private GridViewWithHeaderAndFooter gridView;
    private View emptyView;

    public PtrWrapGridView(Context context) {
        super(context);
        initViews();
    }

    public PtrWrapGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews();
    }

    public PtrWrapGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initViews();
    }

    private void initViews() {
        mPtrClassicHeader = new PtrEHeader(getContext());
        gridView = new GridViewWithHeaderAndFooter(getContext());
        loadMoreView = new LoadMoreView(getContext());
        loadMoreView.setLayoutParams(new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        setRatioOfHeaderHeightToRefresh(1.0f);
        setKeepHeaderWhenRefresh(true);
        setHeaderView(mPtrClassicHeader);
        setContentView(gridView);
        addPtrUIHandler(mPtrClassicHeader);
        setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (null != onRefreshListener && !isLoading) {
                    isLoading = true;
                    onRefreshListener.onRefresh();
                }
            }
        });

        gridView.setOnScrollListener(new LScrollListener());
        gridView.addFooterView(loadMoreView);
        loadMoreView.setState(LoadMoreView.STATE_NOT_ENABLED);
        loadMoreView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    private void setContentView(View content) {
        if (mContent != null && content != null && mContent != content) {
            removeView(mContent);
        }
        mContent = content;
        addView(content);
    }

    public GridViewWithHeaderAndFooter getGridView() {
        return gridView;
    }

    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
    }

    @Override
    protected void onPositionChange(boolean isInTouching, byte status, PtrIndicator mPtrIndicator) {
        super.onPositionChange(isInTouching, status, mPtrIndicator);
        if (null != emptyView) {
            int currentPos = mPtrIndicator.getCurrentPosY();
            emptyView.scrollTo((int) (emptyView.getX() / 2), -currentPos);
        }
    }

    public void onRefreshComplete() {
        isLoading = false;
        refreshComplete();
        if (loadMoreEnabled) {
            loadMoreView.setState(LoadMoreView.STATE_COMPLETE);
        }
    }

    public void setLoadingMoreEnabled(boolean enabled) {
        loadMoreEnabled = enabled;
        loadMoreView.setState(LoadMoreView.STATE_NOT_ENABLED);
    }

    @Override
    public void autoRefresh() {
        gridView.smoothScrollToPosition(0);
        super.autoRefresh();
    }

    public void setRefreshListener(PtrOnRefreshListener onRefreshListener) {
        this.onRefreshListener = onRefreshListener;
    }

    private class LScrollListener implements AbsListView.OnScrollListener {
        private int mLastItemIndex;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE
                    && mLastItemIndex == view.getAdapter().getCount()
                    && !gridView.canScrollVertically(1) && loadMoreEnabled) {
                loadMoreView.setState(LoadMoreView.STATE_LOADING);
                if (null != onRefreshListener && !isLoading) {
                    isLoading = true;
                    onRefreshListener.onLoadMore();
                }
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            mLastItemIndex = firstVisibleItem + visibleItemCount;
        }
    }

}
